package com.xiaoshi.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ResUtil {
    public static final List<Class> basicsType = Arrays.asList(String.class,int.class,Integer.class,double.class,Double.class,char.class,long.class,Long.class);


    /**
     * 判断是不是基础类型
     * @param obj
     * @return
     */
    public static boolean isBasicsType(Object obj){
        return basicsType.contains(obj.getClass());
    }

    /**
     * 设置响应
     *
     * @param response    HttpServletResponse
     * @param contentType content-type
     * @param jsonResult  响应内容
     * @throws IOException IOException
     */
    public static void makeJsonResult(HttpServletResponse response, String contentType,
                                      JsonResult jsonResult) throws IOException {
        response.setContentType(contentType);
        response.setStatus(jsonResult.getStatus());
        response.getOutputStream().write(GsonUtils.toJson(jsonResult).getBytes());
    }



    /**
     * 获取HttpServletRequest
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
}
